package main

import (
	"fmt"
	"math"
	"strings"
)

func IntToBinary(n int32) string {
	a := uint32(n)               //int32转换为uint32
	sb := strings.Builder{}      //拼接字符串
	c := uint32(math.Pow(2, 31)) //
	for i := 0; i < 32; i++ {
		if a&c > 0 { //a和c按位与
			sb.WriteString("1")
		} else {
			sb.WriteString("0")
		}
		c >>= 1 //c右移一位
	}
	return sb.String() //返回拼接后的字符串
}

func main() {
	fmt.Printf("整数260转换32位二进制数：%v\n", IntToBinary(260))
	fmt.Printf("整数-260转换32位二进制数：%v\n", IntToBinary(-260))
	fmt.Printf("整数130转换32位二进制数：%v\n", IntToBinary(130))
	fmt.Printf("整数-130转换32位二进制数：%v\n", IntToBinary(-130))
}
